package com.mycompany.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.mycompany.entity.Mom;
import com.mycompany.service.MomService;

/**
 * Handles requests for the application home page.
 */
@Controller
public class MomController {
	
	private static final Logger logger = LoggerFactory.getLogger(MomController.class);
	
	
	@Autowired
	private MomService momService;
    
	@RequestMapping(value = "/mom")
	public String momHome(Model model) {
	    int totlePrice = new Integer(momService.getTotlePriceRound());
		List<Mom> moms = momService.getAllMomRound();
		Map<String,Integer> mapTop = new HashMap<String, Integer>();
	    Map<String,Integer> mapDown = new HashMap<String, Integer>();
		int buffPrice = 0;
		Iterator<Mom> iterator = moms.iterator();
		while(iterator.hasNext()){
		    Mom m = (Mom)iterator.next();
            if ("top".equals(m.getDecs()))
            {   
                buffPrice = m.getPrice();
                if(mapTop.containsKey(m.getNum())){
                    buffPrice+= mapTop.get(m.getNum());
                }
                mapTop.put(m.getNum(),buffPrice);
            }

            else
            {
                buffPrice = m.getPrice();
                if(mapDown.containsKey(m.getNum())){
                    buffPrice+= mapDown.get(m.getNum());
                }
                mapDown.put(m.getNum(),buffPrice);
            }
		}
		model.addAttribute("mapTop", mapTop );
		model.addAttribute("mapDown", mapDown );
		model.addAttribute("totlePrice", totlePrice);
		model.addAttribute("totlePrice20Percen", (totlePrice - ((totlePrice*20)/100)));
		model.addAttribute("mapTopCount", mapTop.size());
        model.addAttribute("mapDownCount", mapDown.size());
		return "mom/list";
	}
	
	@RequestMapping(value = "/addMom", method = RequestMethod.GET)
    public String momAdd(Model model) {
	    List<Mom> moms = momService.getAllMom();
	      model.addAttribute("moms", moms );
        return "mom/add";
    }
	@RequestMapping(value = "/addMom", method = RequestMethod.POST)
    public String momAdd(@RequestParam("num") String num,@RequestParam("decs") String decs,@RequestParam("price") int price,Model model) {
	  Mom std = new Mom();
      std.setNum(num);
      std.setDecs(decs);
      std.setPrice(price);
      std.setTo_date(new Date());
      momService.save(std);
      model.addAttribute("momName", std.getNum());
      List<Mom> moms = momService.getAllMom();
      model.addAttribute("moms", moms );
      return "mom/add";
    }
	@RequestMapping(value = "/editMom", method = RequestMethod.GET)
    public String momEdit(@RequestParam("id") String id,Model model) {
	    Mom std = momService.getMom(new Integer(id));
	    model.addAttribute("mom", std);
	    model.addAttribute("id", std.getId());
        return "mom/edit";
    }
	
	@RequestMapping(value = "/editMom", method = RequestMethod.POST)
    public String momEdit(@RequestParam("id") String id,@RequestParam("num") String num,@RequestParam("decs") String decs,@RequestParam("price") int price,Model model) {
	    Mom std = momService.getMom(new Integer(id));
	    std.setNum(num);
	    std.setDecs(decs);
	    std.setPrice(price);
	    std.setTo_date(new Date());
	    momService.update(std);
	    model.addAttribute("mom", std);
        model.addAttribute("idEdited", std.getId());
        return "mom/edit";
    }
	
	@RequestMapping(value = "/deleteMom", method = RequestMethod.GET)
    public String momDelete(@RequestParam("id") String id,Model model) {
        
	    Mom std = momService.getMom(new Integer(id));
	    momService.delete(std);
	    List<Mom> moms = momService.getAllMom();
	    model.addAttribute("deleteId", std.getId());
	    model.addAttribute("deleteMom", std);
        model.addAttribute("moms", moms );
        return "mom/add";
    }
	
	
	
	
}
